import Vue from "vue";
import Vuex from "vuex";
import Cart from "./Cart";
import Product from "./Product";
Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    user: "lbw",
        num: 666,
        products: [],
        msg: "那是一条神奇的天路哟"
  },
  getters: {
    reverseMsg(state){
      return state.msg.split("").reverse().join("")
    }
  },
  mutations: {
    INCREMENT_NUM(state,payload){
      state.num = state.num+payload;
    },
    DECREMENT_NUM(state,payload){
      state.num = state.num-payload;
    },
    SET_PRODUCT(state,payload){
      state.products=payload;
  },
},
  actions: {
    GET_PRODUCT(context, payload) {
      console.log(context)
      console.log(payload)
      fetch('http://chst.vip/data/index.json')
          .then(body => body.json())
          .then(res => {
              console.log(res)
              context.commit("SET_PRODUCT", res.data)
          })
        }
  },
  modules: {
    Cart,
    Product
  },
  // plugins:{}
});
